package com.xiaochong.tir.api.component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.api.util.HttpUtils;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by org on 2018/6/11.
 * 获取人民币/美元汇率
 */
@Component
public class CnyToUsdRateComponent {
    @Value("${cnyToUsdRate.url}")
    private String cnyToUsdRate;

    @Value("${cnyToUsdRate.huobiproUrl}")
    private String cnyToUsdRateHuobiproUrl;
    @Resource
    private RedisServer redisServer;
    public String getCnyUsdRate(){
        LogTrace.beginTrace();
        String rate = "0";
        try {
            String data="";
            boolean successFlag = false;
            //首先去获取货币的usdt_cny，如果获取不到在去原来地址获取
            data = HttpUtils.get(cnyToUsdRateHuobiproUrl);
            if(StringUtils.isNotBlank(data)){
                JSONObject jsonObjectH = JSON.parseObject(data);
                Integer objectCode = (Integer) jsonObjectH.get("code");
                Object objectData = jsonObjectH.get("data");
                boolean objectSuccess = (boolean) jsonObjectH.get("success");
                if(objectCode.intValue() == 200 && null != objectData && objectSuccess){
                    List<Map<String,Object>> jsonArray = (List<Map<String, Object>>) objectData;
                    if(jsonArray != null && jsonArray.size()>0){
                        for(Map<String,Object> map:jsonArray){
                            String name = (String) map.get("name");
                            BigDecimal rete2 = (BigDecimal) map.get("rate");
                            if("usdt_cny".equals(name)){
                                rate =rete2.stripTrailingZeros().toPlainString();
                                successFlag = true;
                            }
                        }
                    }
                }
            }
            if(!successFlag){
                //获取指定接口数据
                data = HttpUtils.get(cnyToUsdRate);
                if(StringUtils.isNotBlank(data)){
                    JSONObject jsonObject = JSON.parseObject(data);
                    Map<String,Object> rateObject = (Map<String, Object>) jsonObject.get("data");
                    rate = String.valueOf(rateObject.get("rate"));
                }
            }
            if(StringUtils.isNotBlank(rate)){
                redisServer.set(RedisKeysUtils.RMB_USD_RATE,rate);
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("tir-server获取人民币/美元汇率",e);
        }
        return  rate;
    }
}
